package by.wind.drebedengi.datalayer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;

import by.wind.drebedengi.datalayer.bean.Record;

public class SetRecordList  extends AbstractAction{
	
	public static final String SOAP_ACTION = "setRecordList";
	public static final String METHOD_NAME = "setRecordList";
	
	private ArrayList<Record> itsRecordList;
	
	private ArrayList<SetRecordElementResult> itsSetRecordListResult = new ArrayList<SetRecordElementResult>();
	
	public SetRecordList(ArrayList<Record> anRecordList) {
		itsRecordList = anRecordList;
	}
	
	public ArrayList<SetRecordElementResult> getSetRecordListResult() {
		return itsSetRecordListResult;
	}
	
	@Override
	public void loadData() throws Exception {
		
		HashMap<String, Object> propertyList = new HashMap<String, Object>();
		propertyList.put(Constants.API_ID_KEY, Constants.API_ID_VALUE);
		propertyList.put(Constants.LOGIN_KEY, Constants.LOGIN_VALUE);
		propertyList.put(Constants.PASS_KEY, Constants.PASS_VALUE);
		
		propertyList.put(Constants.LIST, convertRecordArrayToHashTables(itsRecordList));
		Vector<Hashtable<String,String>> itsData = (Vector<Hashtable<String, String>>) loadData(METHOD_NAME, SOAP_ACTION, propertyList);
		for(int i = 0; i<itsData.size(); i++) {
			itsSetRecordListResult.add(new SetRecordElementResult(itsData.get(i)));
		}
		
		
		
	}
	
	Vector<Hashtable<String,Object>> convertRecordArrayToHashTables(ArrayList<Record> anRecordList) {
		
		Vector<Hashtable<String,Object>> result = new Vector<Hashtable<String,Object>>();
		
		for(Record recordElement : anRecordList) {
			Hashtable<String,Object> hashtableElement = new Hashtable<String,Object>();
			
			if (recordElement.getClient_id() != 0) {
				hashtableElement.put(Record.CLIENT_ID, recordElement.getClient_id());
			} else {
				hashtableElement.put(Record.SERVER_ID, recordElement.getServer_id());
			}
			hashtableElement.put(Record.PLACE_ID, recordElement.getPlace()!=null?recordElement.getPlace().getServer_id():0);
			
			if (recordElement.getOperation_type() == Constants.OPERATION_WASTLE) {
				hashtableElement.put(Record.BUDGET_OBJECT_ID, recordElement.getCategory()!= null?recordElement.getCategory().getServer_id():0);
			} else if (recordElement.getOperation_type() == Constants.OPERATION_INCOME) {
				hashtableElement.put(Record.BUDGET_OBJECT_ID, recordElement.getSource()!= null?recordElement.getSource().getServer_id():0);
			}
			hashtableElement.put(Record.SUM, recordElement.getSum());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
			hashtableElement.put(Record.OPERATION_DATE, sdf.format(recordElement.getOperation_date()));
			hashtableElement.put(Record.COMMENT, recordElement.getComment());
			hashtableElement.put(Record.CURRENCY_ID, recordElement.getCurrency().getServer_id());
			hashtableElement.put(Record.IS_DUTY, recordElement.isIs_duty());
			hashtableElement.put(Record.COMMENT, recordElement.getComment());
			hashtableElement.put(Record.OPERATION_TYPE, recordElement.getOperation_type());
			
			result.add(hashtableElement);
		}
		return result;
	}
	
	
}
